package cc.lj.internal.payment.application.settlement.service;

import cc.lj.internal.api.coupons.dto.CouponsSettlementApiDTO;
import cc.lj.internal.api.coupons.dto.TicketPayRecordApiDTO;
import cc.lj.internal.api.coupons.dto.command.CouponsSettlementApiCommand;
import cc.lj.internal.api.coupons.dto.command.TicketPayRecordApiUpdateCommand;
import cc.lj.internal.api.coupons.dto.querty.TicketPayRecordApiQuery;
import cc.lj.internal.api.payment.command.TicketSettlementApiCommand;
import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.payment.application.FeignAppService;
import cc.lj.internal.payment.domain.pay.repository.IProductConumeRepository;
import cc.lj.internal.payment.domain.pay.service.IPayFormListDomainService;
import cc.lj.internal.payment.domain.pay.service.IProductConumeDomainService;
import cc.lj.internal.payment.domain.pay.service.ITicketPayRecordDomainService;
import cc.lj.internal.payment.domain.pay.vo.CouponsSettlementDO;
import cc.lj.internal.payment.domain.pay.vo.PayformListDO;
import cc.lj.internal.payment.domain.pay.vo.ProductConumeDO;
import cc.lj.internal.payment.domain.pay.vo.TicketPayRecordDO;
import cc.lj.internal.payment.domain.settlement.service.ICouponsCapLogDomainService;
import cc.lj.internal.payment.domain.settlement.service.ICouponsSubsidyDetailDomainService;
import cc.lj.internal.payment.domain.settlement.service.ICouponsSubsidyDomainService;
import cc.lj.internal.payment.domain.settlement.service.ITicketSettlementDomainService;
import cc.lj.internal.payment.domain.settlement.vo.CouponsCapLogDO;
import cc.lj.internal.payment.domain.settlement.vo.CouponsSubsidyDO;
import cc.lj.internal.payment.domain.settlement.vo.CouponsSubsidyDetailDO;
import cc.lj.internal.payment.domain.settlement.vo.MartCapBatchDO;
import cc.lj.internal.payment.infrastructure.exception.PaymentExceptionEnum;
import cc.lj.internal.payment.infrastructure.po.ProductConumePO;
import cc.lj.internal.payment.interfaces.dto.convert.CouponsSettlementDTOConvertDO;
import cc.lj.internal.payment.interfaces.dto.convert.TicketPayRecordDTOConvertDO;
import lj.internal.dict.enums.EnumTicketPayRecordIsNewStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 电子券结算　app service
 * @author liangxifeng
 * @date 2022-01-13
 */
@Slf4j
@Service(value = "ticketSettlementAppService" )
public class TicketSettlementAppService {
    //调用第三方接口 feign app service
    @Resource(name = "feignAppService")
    private FeignAppService feignAppService;

    //合同收款表 domain
    @Resource(name = "productConumeDomainServiceImpl")
    private IProductConumeDomainService productConumeDomainService;

    //电子券分摊 domain
    @Resource(name = "couponsSubsidyDomainServiceImpl")
    private ICouponsSubsidyDomainService subsidyDomainService;
    //电子券分摊明细 domain
    @Resource(name = "couponsSubsidyDetailDomainServiceImpl")
    private ICouponsSubsidyDetailDomainService detailDomainService;

    //现金+电子券支付方式表 domain
    @Resource(name = "payFormListDomainServiceImpl")
    private IPayFormListDomainService payFormListDomainService;

    //生成电子券计算日志表领域
    @Resource(name = "couponsCapLogDomainServiceImpl")
    private ICouponsCapLogDomainService logDomainService;

    /**
     * 电子券结算对外入口主方法
     * @param apiCommand
     * @return Boolean true:结算成功，false:结算失败
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public Object doMain(TicketSettlementApiCommand apiCommand){
        /**
         * 1.通过合同收款表主键查询券分摊表，验证幂等
         */
        if( checkSubsidy(apiCommand.getConsumeId()) == true ) {
            throw new ServiceException(PaymentExceptionEnum.COUPONS_SUBSIDY_EXIST);
        }
        /**
         *  2.去电子券模块，通过consumeId查询该合同下已收的电子券数据
         */
        TicketPayRecordApiQuery payRecordApiQuery = new TicketPayRecordApiQuery();
        payRecordApiQuery.setPactId(apiCommand.getConsumeId());
        List<TicketPayRecordApiDTO> payRecordList  = feignAppService.getList(payRecordApiQuery);
        log.info("电子券补贴结算,通过consumeId:{},查询tickat_pay_record返回结果:{}",apiCommand.getConsumeId(),payRecordList);
        if(payRecordList.size() == 0) {
            return "db_ticket.ticket_pay_record合同收券记录表没有收券数据，无需核销券分摊";
        }
        List<TicketPayRecordDO> listDO =
                payRecordList.stream().map( x-> TicketPayRecordDTOConvertDO.INSTANCE.convert(x)).collect(Collectors.toList());
        /**
         * 3.结算分摊，计算最终电子券分摊，向电子券分摊表，分摊明细表新增数据，修改payform_list表中商户分摊总额，新增电子券分摊日志表数据
         */
        Integer subsidyId = settlementAction(apiCommand,listDO);
        return subsidyId;
    }

    /**
     * 电子券结算对外入口主方法
     * @param apiCommand
     * @Return 返回电子券分摊表主键
     */
    public Integer settlementAction(TicketSettlementApiCommand apiCommand,List<TicketPayRecordDO> payRecordList){
        /**
         * 通过consumerId查询product_conume表
         */
        ProductConumeDO conumeDO = productConumeDomainService.getById(apiCommand.getConsumeId());
        log.info("电子券补贴结算,通过consumeId:{},查询product_conume返回结果:{}",apiCommand.getConsumeId(),conumeDO);

        //本商户对应各个活动的已收款卖场分摊map, key=封顶额活动主键，value=已收款卖场分摊总额
        Map<Integer,BigDecimal> capMartSumMap = new HashMap<>();
        //本商户对应各个活动下，各个批次的已收款卖场分摊map, key=封顶额活动表主键，value=(key=电子券批次表主键,value=已收款卖场分摊总额)
        Map<Integer, Map<Integer,BigDecimal>> capBatchMartSumMap = new HashMap<>();

        //商户总分摊额
        BigDecimal merchantTotalPrice = new BigDecimal(0);
        //卖场总分摊额
        BigDecimal martTotalPrice = new BigDecimal(0);
        //电子券总额
        BigDecimal totalPrice = new BigDecimal(0);
        //电子券售券总额
        BigDecimal sellTotalPrice = new BigDecimal(0);
        //最终新增券分摊明细表数据 List
        List<CouponsSubsidyDetailDO> finalDetailDOList = new ArrayList<>();
        //循环合同收券，分摊结算处理
        Integer i = 1;
        for (TicketPayRecordDO queryPayRecoreDO : payRecordList) {
            CouponsSettlementApiCommand settlementApiCommand = new CouponsSettlementApiCommand();
            settlementApiCommand.setBatchId(queryPayRecoreDO.getTicketBatchId());
            settlementApiCommand.setMerchantShow(conumeDO.getConsumeMerchantShow());
            //去电子券模块查询电子券分摊数据（封顶额等）
            CouponsSettlementApiDTO settlementApiDTO = feignAppService.getCouponsSettlementData(settlementApiCommand);
            CouponsSettlementDO settlementDO = CouponsSettlementDTOConvertDO.INSTANCE.convert(settlementApiDTO);
            log.info("+++第"+(i)+"张券,券批次ID:"+queryPayRecoreDO.getTicketBatchId()+
                    ",券编号:"+queryPayRecoreDO.getTicketUsecode()+
                    ",去电子券服务获取封顶额分摊数据结果: " +settlementDO);
            //生成最终分摊数据
            CouponsSubsidyDetailDO detailDO = createFinalData(settlementDO, capMartSumMap,capBatchMartSumMap,i);
            i++;
            detailDO.setAddtime(LocalDateTime.now());
            detailDO.setConsumeId(conumeDO.getConsumeId()); //合同收款表主键
            detailDO.setMerchantShow(settlementDO.getMerchantShow()); //商户展位号
            detailDO.setTicketUsecode(queryPayRecoreDO.getTicketUsecode()); //电子券编码
            detailDO.setTicketPrice(queryPayRecoreDO.getTicketPrice()); //券面额
            //log.info("该批次batchId="+queryPayRecoreDO.getTicketBatchId()+",最终分摊数据:"+detailDO);
            finalDetailDOList.add(detailDO);

            //累加卖场分摊总额
            addMartSum(detailDO,capMartSumMap,capBatchMartSumMap);

            //累加商户总分摊额和卖场分摊总额
            merchantTotalPrice = merchantTotalPrice.add(detailDO.getMerchantShareValue());
            martTotalPrice = martTotalPrice.add(detailDO.getMarketShareValue());
            totalPrice = totalPrice.add( queryPayRecoreDO.getTicketPrice() );
            sellTotalPrice = sellTotalPrice.add( detailDO.getSellPrice());
        }

        //组装券分摊表数据
        CouponsSubsidyDO subsidyDO = new CouponsSubsidyDO();
        subsidyDO.setPactNumber(String.valueOf(conumeDO.getConsumePactId())); //合同号
        subsidyDO.setCouponsValue(totalPrice);//电子券总额度coupons_value
        subsidyDO.setMerchantShareValue(merchantTotalPrice);//商户总实际分摊额总额
        subsidyDO.setMarketShareValue(martTotalPrice);//卖场总实际分摊额总额
        subsidyDO.setMerchantShow(conumeDO.getConsumeMerchantShow()); //展位号
        subsidyDO.setAddtime(LocalDateTime.now()); //记录新增时间
        subsidyDO.setConsumeId(conumeDO.getConsumeId()); //关联合同表主键
        subsidyDO.setMerchantId(conumeDO.getConsumeMerchantId()); //商户信息表主键
        subsidyDO.setSellTotalPrice(sellTotalPrice); //电子券售券总额
        log.info("商户："+conumeDO.getConsumeMerchantShow()+",合同号:"+conumeDO.getConsumePactId()+"," +
                ",商户分摊总额="+merchantTotalPrice+
                ",卖场分摊总额="+martTotalPrice+
                ",券总额="+totalPrice);
        log.info("最终新增券分摊表数据:"+subsidyDO);
        log.info("最终新增券分摊明细表数据:"+finalDetailDOList);
        //新增电子券补贴表数据
        Integer subsidyId = subsidyDomainService.insert(subsidyDO);
        for (CouponsSubsidyDetailDO detailDO : finalDetailDOList) {
            //新增补贴明细表数据
            Integer detailID = detailDomainService.insert(detailDO);
            CouponsCapLogDO logDO = detailDO.getCouponsCapLogDO();
            //新增封顶额日志表数据
            if( logDO != null ) {
                logDO.setSubsidyDetailId(detailID);
                logDomainService.insert(logDO);
            }
        }

        //通过consumeId修改电子券+现金表中总商户分摊字段
//        PayformListDO payformListDO = new PayformListDO();
//        payformListDO.setPayformConsumeId(apiCommand.getConsumeId());
//        payformListDO.setPayformTradeFee(merchantTotalPrice);
//        payFormListDomainService.updateByConsumeId(payformListDO);
        return subsidyId;

    }

    /**
     * 验证该合同是否已经在分摊表中存在数据
     * @param consumeId
     * @return true 存在，false 不存在
     */
    private boolean checkSubsidy(Integer consumeId) {
        CouponsSubsidyDO subsidyDO = new CouponsSubsidyDO();
        subsidyDO.setConsumeId(consumeId);
        List<CouponsSubsidyDO> subsidyDOList = subsidyDomainService.getList(subsidyDO);
        return subsidyDOList.size() > 0 ? true : false;
    }

    /**
     * 累加已收款卖场分摊总和
     * @param recordDO 合同收券表DO
     * @param capMartSumMap 各活动已收款卖场分摊总额
     * @param capBatchMartSumMap 各活动各批次卖场分摊总额
     */
    private void addMartSum(CouponsSubsidyDetailDO recordDO ,
                            Map<Integer,BigDecimal> capMartSumMap,
                            Map<Integer,Map<Integer,BigDecimal>> capBatchMartSumMap)
    {

        if(recordDO.getCapId() == null || recordDO.getCapId() == 0) {
            return;
        }
        //该活动，已收款卖场分摊总额累加最新分摊
        if(capMartSumMap.get(recordDO.getCapId()) == null ) {
            capMartSumMap.put(recordDO.getCapId(), new BigDecimal(0));
        }
        BigDecimal martSum = recordDO.getMarketShareValue().add(capMartSumMap.get(recordDO.getCapId()) );
        capMartSumMap.put(recordDO.getCapId(), martSum);


        //获取该活动下，所有批次对应的已收款卖场分摊总额map
        Map<Integer,BigDecimal> batchMartMap = capBatchMartSumMap.get(recordDO.getCapId());
        //如果对应批次没有，则初始化该批次已收款卖场分摊总额=0
        if(batchMartMap.get(recordDO.getBatchId()) == null ) {
            batchMartMap.put(recordDO.getBatchId(), new BigDecimal(0));
        }
        //该活动，该批次已收款卖场分摊总额累加最新分摊
        BigDecimal batchMartSum = recordDO.getMarketShareValue().add(batchMartMap.get(recordDO.getBatchId()) );
        batchMartMap.put(recordDO.getBatchId(),batchMartSum);
        capBatchMartSumMap.put(recordDO.getCapId(),batchMartMap);
    }

    /**
     * 生成最终分摊数据
     * 去数据库查询该商户，该活动已收款卖场分摊总额（已收款大封顶额），该商户该批次卖场分摊总额（已收款小封顶额）
     * 比较该商户该批次可参加的多个活动的 已收款大封顶额 以及 小封顶额，最终计算出本批次券需要的分摊数据
     * @param settlementDO 分摊数据
     * @param capMartSumMap 各活动已收款卖场分摊总额
     * @param capBatchMartSumMap 各活动各批次卖场分摊总额
     * @param curTiketNum 遍历到第几张券了
     */
    private CouponsSubsidyDetailDO createFinalData(CouponsSettlementDO settlementDO,
                            Map<Integer,BigDecimal> capMartSumMap,
                            Map<Integer,Map<Integer,BigDecimal>> capBatchMartSumMap,Integer curTiketNum)
    {
        // 组装原始分摊数据
        CouponsSubsidyDetailDO detailDO = new CouponsSubsidyDetailDO();
        detailDO.setMarketRatio(settlementDO.getMartOldShareRatio()); //原始卖场分摊比
        detailDO.setMarketShareValue(settlementDO.getMartOldSharePrice()); //原始卖场分摊金额
        detailDO.setMerchantRatio(settlementDO.getMerchantOldShareRatio()); //原始商户分摊比
        detailDO.setMerchantShareValue(settlementDO.getMerchantOldSharePrice()); //原始商户分摊金额
        detailDO.setBatchId(settlementDO.getTicketBatchId()); //电子券批次主键
        detailDO.setShareType(0); //默认是原始分摊 0:原始分摊，1:新分摊
        detailDO.setSellPrice(settlementDO.getSellPrice()); //电子券售券金额

        //无封顶额活动，不需要计算
        if(settlementDO.getMartCapBatchDTOList() == null) {
            return detailDO;
        }
        /**
         * 一个商户同一个批次，可以同时参加多个封顶额活动，
         * 这里遍历可参加的多个封顶额活动（活动期内，且按着开始时间,活动主键正序）
         * 遍历中，如果当前活动封顶额未打满,则采用原始分摊计算
         * 遍历中，如果当前活动封顶额打满则continue,继续下次循环，
         * 如果所有活动封顶额都打满，则采用最后一个封顶额活动打满后的新分摊计算
         * 注意：这里其实可以直接取最后一个活动数据直接计算，无需循环，考虑今后可能会变规则，如果在遍历中添加其他退则这里直接修改即可
         */
        Integer martCapBatchSize = settlementDO.getMartCapBatchDTOList().size();
        log.info("第"+curTiketNum+"张券有"+martCapBatchSize+"个活动,开始计算各个活动的封顶额...");
        //获取多个活动数据中最后一条数据的key
        Integer lastKey = martCapBatchSize - 1;
        for (int i = 0; i < martCapBatchSize; i++) {
            MartCapBatchDO martCapBatchDO = settlementDO.getMartCapBatchDTOList().get(i);
            CouponsSubsidyDetailDO detailDO1 = new CouponsSubsidyDetailDO();
            detailDO1.setShareType(0); //分摊类型，只查询原始分摊
            //如果该活动，已收款卖场分摊总额没有查询过,则进行查询
            if(!capMartSumMap.containsKey(martCapBatchDO.getCapId())) {
                detailDO1.setMerchantShow(settlementDO.getMerchantShow());
                detailDO1.setCapId(martCapBatchDO.getCapId());
                //查询该商户，在该封顶额活动中已经收款卖场分摊总额
                BigDecimal martSum = detailDomainService.getMartSharePriceSum(detailDO1);
                capMartSumMap.put(martCapBatchDO.getCapId(), martSum);
                capBatchMartSumMap.put(martCapBatchDO.getCapId(),new HashMap<>());
            }

            //获取该活动下，所有批次对应的已收款卖场分摊总额map
            Map<Integer,BigDecimal> batchMartMap = capBatchMartSumMap.get(martCapBatchDO.getCapId());
            //如果对应批次没有，则查询数据库获取该批次，该活动，该商户已收款卖场分摊总额
            if(batchMartMap.get(settlementDO.getTicketBatchId()) == null ) {
                detailDO1.setMerchantShow(settlementDO.getMerchantShow());
                detailDO1.setCapId(martCapBatchDO.getCapId());
                detailDO1.setBatchId(settlementDO.getTicketBatchId());
                BigDecimal batchMartSum = detailDomainService.getMartSharePriceSum(detailDO1);
                batchMartMap.put(settlementDO.getTicketBatchId(),batchMartSum);
                capBatchMartSumMap.put(martCapBatchDO.getCapId(),batchMartMap);
            }
            log.info("==第"+curTiketNum+"张券,第"+(i+1)+"个活动计算封顶额start,活动Id:"+martCapBatchDO.getCapId()+",大分摊map:"+capMartSumMap+"，小分摊map:"+capBatchMartSumMap);
            CouponsCapLogDO logDO = new CouponsCapLogDO(); //封顶额生成日志表DO
            logDO.setCreateTime(LocalDateTime.now());
            /**
             * 1.大分摊达到封顶额的情况
             * 商户总分摊金额达到卖场封顶额情况：原卖场分摊+已收款卖场总分摊 >商户本期活动封顶额
             */
            //该活动已收款总额
            BigDecimal martSum = capMartSumMap.get(martCapBatchDO.getCapId());
            //原卖场分摊+已收款卖场总分摊
            BigDecimal totalShare = settlementDO.getMartOldSharePrice().add(martSum);
            log.info("计算大分摊==" +
                    "商户:"+settlementDO.getMerchantShow()+
                    ",活动ID:"+martCapBatchDO.getCapId()+
                    ",A原始分摊="+settlementDO.getMartOldSharePrice().floatValue()+",B活动累计分摊="+martSum+",A+B="+totalShare.floatValue()+",C封顶额="+martCapBatchDO.getCapValue());
            //如果是最后一条数据了，达到封顶额情况,则使用新分摊
            if( totalShare.floatValue() > martCapBatchDO.getCapValue() ) {
                log.info("大分摊 A+B > C");
                if( i == lastKey) {
                    createNewShareDate(detailDO,martCapBatchDO);
                    logDO.setShareType(1); //日志超过整体封顶额
                    logDO.setCapDynamicValue(martSum); //动态封顶额总和=该活动已收款总额
                    detailDO.setCouponsCapLogDO(logDO);
                    log.info("商户:"+martCapBatchDO.getMerchantShow()+",循环到最后一个活动,活动主键="+martCapBatchDO.getCapId()+
                            "且大分摊达到封顶额,使用新分摊:"+detailDO);
                    break;
                } else {
                    log.info("商户:"+martCapBatchDO.getMerchantShow()+",活动主键="+martCapBatchDO.getCapId()+
                            "大分摊达到封顶额,继续计算下一个活动封顶额");
                    continue;
                }

            }
            if(totalShare.floatValue() <= martCapBatchDO.getCapValue() ) {
                log.info("大分摊 A+B <= C 未达大到封顶额,接着计算小分摊");
            } else {
                log.info("大分摊 A+B > C 达到了大封顶额，但不是最后一个活动,接着计算小分摊");
            }



            /**
             * 2. 小封顶额达到的情况
             **/
            //计算该批次占有卖场最高封顶额度 = 占有卖场最高封顶额比例 * 该商户本期活动封顶额
            BigDecimal batchMaxCapValue = martCapBatchDO.getMartCapRatio().multiply(new BigDecimal(martCapBatchDO.getCapValue()));
            //log.info("商户："+martCapBatchDO.getMerchantShow()+",批次主键："+martCapBatchDO.getTicketBatchId()+",在该活动主键:"+martCapBatchDO.getCapId()+",最高占用的封顶额="+batchMaxCapValue.floatValue());

            //查询该活动下所有电子券批次已收款卖场分摊电子券总额map
            Map<Integer,BigDecimal> batchMap = capBatchMartSumMap.get(martCapBatchDO.getCapId());
            //该批次已收款总额
            BigDecimal batchMartSum = null;
            if( batchMap != null ) {
                batchMartSum = batchMap.get(settlementDO.getTicketBatchId());
            }
            //log.info("##电子券收款结算分摊，商户:"+settlementDO.getMerchantShow()+",已活动收款卖场分摊map："+capMartSumMap+",活动批次已收款卖场分摊map="+capBatchMartSumMap);
            //原卖场分摊+该批次已收款卖场总分摊
            BigDecimal batchTotalShare = settlementDO.getMartOldSharePrice().add(batchMartSum);
            log.info("计算小分摊==商户:"+settlementDO.getMerchantShow()+
                    ",活动ID:"+martCapBatchDO.getCapId()+
                    ",批次ID:"+settlementDO.getTicketBatchId()+
                    ",F最高可占用额度="+batchMaxCapValue.floatValue()+
                    ",D原始分摊="+settlementDO.getMartOldSharePrice().floatValue()+",E活动批次累计分摊="+batchMartSum+",D+E="+batchTotalShare.floatValue());
            /**
             * 达到最高封顶额情况：原卖场分摊+该批次已收款卖场总分摊 > 该批次占有卖场最高封顶额
             */
            if( batchTotalShare.floatValue() > batchMaxCapValue.floatValue() ) {
                log.info("小分摊 D+E > F, 达到封顶额");
                if(  i == lastKey ) {//最后一条数据了，达到封顶额情况,则使用新分摊
                    createNewShareDate(detailDO,martCapBatchDO);
                    logDO.setShareType(2); //超过批次封顶额
                    logDO.setCapDynamicValue(batchMartSum); //动态封顶额总和=该批次已收款总额
                    detailDO.setCouponsCapLogDO(logDO);
                    log.info("商户:"+martCapBatchDO.getMerchantShow()+",循环到最后一个活动,活动主键="+martCapBatchDO.getCapId()+
                            "且小分摊达到封顶额,使用新分摊:"+detailDO);
                    break;
                } else { //如果本次循环大小封顶额都达到了，且不是最后一条数据，则终止本次循环，继续下一次循环
                    log.info("商户:"+martCapBatchDO.getMerchantShow()+",活动主键="+martCapBatchDO.getCapId()+
                            "大小封顶额都达到了,且不是最后一个活动，继续下次循环，计算封顶额");
                    continue;
                }
            }else {
                //如果本次循环大封顶和小封顶额都没有达到，则该批次券的分摊按原始分摊计算，占用的封顶额额度是本次循环的封顶额活动的额度
                detailDO.setCapId(martCapBatchDO.getCapId());
                logDO.setShareType(0); //使用默认原始分摊
                logDO.setCapDynamicValue(batchMartSum); //动态封顶额总和=该批次已收款总额
                detailDO.setCouponsCapLogDO(logDO);
                log.info("小分摊 D+E <= F");
                log.info("商户:"+martCapBatchDO.getMerchantShow()+",活动主键="+martCapBatchDO.getCapId()+
                        "且大小分摊均未达到封顶额,使用原始分摊:"+detailDO);
                break;
            }
        }
        //如果大小封顶额都没有达到，则使用原始分摊计算
        return detailDO;
    }

    /**
     * 组装达到封顶额后的新分摊数据
     * @param detailDO
     * @param martCapBatchDO
     * @return
     */
    private void createNewShareDate(CouponsSubsidyDetailDO detailDO, MartCapBatchDO martCapBatchDO) {
        detailDO.setMarketRatio(martCapBatchDO.getMartNewShareRatio()); //新卖场分摊比
        detailDO.setMarketShareValue(martCapBatchDO.getMartNewSharePrice()); //新卖场分摊金额
        detailDO.setMerchantRatio(martCapBatchDO.getMerchantNewShareRatio()); //新商户分摊比
        detailDO.setMerchantShareValue(martCapBatchDO.getMerchantNewSharePrice()); //新商户分摊金额
        detailDO.setCapId(martCapBatchDO.getCapId()); //封顶额活动主键
        detailDO.setShareType(1); //使用新分摊
    }
}
